//index.js
//获取应用实例
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var config = require('../../base.conf.js').config;
const app = getApp();
var timer = null;
Page({
  data: {
    pageSize: 15,
    currentPage: 1,
    moreType: 'noMore',
    statusType: 'none',
    statuserro: false,
    swiperlowHeight: false,
    swiepCurrent: 0,
    active: 'home',
    banneList: [],
    isdonation: true,
    shareUuid: '',
    shareStep: '',
    myInfo: {},
    shareWxName: '',
    shareWxPortrait: '',
    sendStep: '', //要提交的步数
    option: '',
    isSendBtn: true, //防止重复提交表单
  },
  onLoad: function (option) {
    if (option) {
      this.setData({
        option: option,
        getUserStep: utils.getUserStep(),
      }, () => {
        this.init()
      })
      app.save("shareId", option.shareId || '');
    } else {
      utils.showtoast('没有获取到分享人信息', null, 3000, true)
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }, 1500)
    }
  },
  //更新用户信息
  upDatastepUi(e) {
    this.setData({
      getUserStep: e.detail,
      myInfo: utils.getWechatInfo()
    })
    this.checkMe()
  },
  upstep() {
    var {
      getUserStep,
    } = this.data;
    app.getWeRun((status, msg) => {
      if (status) {
        this.setData({
          getUserStep: msg.data,
          myInfo: utils.getWechatInfo() || null
        })
        this.checkMe();
      } else {
        if (msg && msg.ststus == '该用户不存在或未授权') {
          wx.removeStorageSync('sysStep')
          this.setData({
            getUserStep: ''
          }, () => {
            setTimeout(() => {
              this.init();
            }, 500)
          })
        }
      }
    }, true, true);
  },
  init() {
    this.getSysStep();
    var {
      getUserStep,
      option
    } = this.data;
    if (app.getValueSync('unionId') == '') {
      this.showModal("noUid")
    } else {
      this.upstep();
    }
  },
  getSysStep() {//获取分享人的步数
    var that = this
    var data = {
      uuid: this.data.option.uuid
    }
    Ajax.post('getSysStep',data).then((d) => {
      utils.hideLoading();
      console.log(d)
      if (d.status == 200 && d.data) {
        that.setData({
          shareStep: d.data.sysStep,
          shareWxName: d.data.wxName,
          shareWxPortrait: d.data.wxPortrait,
        })
      } else {
        utils.showtoast(d.data || d.msg)
      }
    }).catch((e) => {
      console.log(e)
      utils.hideLoading()
      utils.showtoast('网络加载失败，刷新重试')
    })
  },
  showModal(id) {//显示或者隐藏弹窗
    var modal = this.selectComponent("#" + (id || 'modal'));
    // if (modal.isShow() && !flag) {
    //   modal.hide()
    // } else {
      modal.show();
    // }
  },
  closeModal(id) { //关闭弹窗
    var modal = this.selectComponent("#" + (id || 'modal'));
    modal.hide();
  },
  stepinputFn(e) {//步数输入框
    console.log(e.detail.value)
    this.setData({
      sendStep: e.detail.value
    })
  },
  stepinputBlurFn() {//步数数字控制
    var sendStepa = this.data.sendStep
    if (sendStepa !== '') {
      this.setData({
        sendStep: parseInt(sendStepa)
      })
    }
  },
  showpopr() {//输入确认
    if (this.data.sendStep == "" && this.data.sendStep == 0) {
      utils.showtoast('请输入正确的步数哦!')
    } else {
      var sendStep = this.data.sendStep
      var myStep = 9999
      if (sendStep > myStep) {
        utils.showtoast('不能超过总步数哦!')
        this.setData({
          sendStep: ''
        })
      } else {
        this.showModal('Model');
      }
    }
  },
  //赠送步数API
  donateStep() {
    var data = {
      fromId: this.data.getUserStep.uuid, //我的id
      toId: this.data.option.uuid, //TA的id
      step: this.data.sendStep
    }
    Ajax.post('donateStep',data).then((d) => {
      utils.hideLoading();
      console.log(d)
      if (d.status == 200&&d.code==0) {
        this.setData({
          isdonation: false
        }, () => {
          this.closeModal('Model');
          timer = setTimeout(() => {
            utils.showLoading();
            this.setData({
              isSendBtn: true,
              isdonation: true,
              sendStep: ""
            }, () => {
              this.init();
              utils.hideLoading();
            })
          }, 2000)
        })
      } else {
        this.setData({
          isSendBtn: true
        })
        this.closeModal('Model');
        utils.showtoast(d.data || d.msg)
      }
    }).catch((e) => {
      console.log(e)
      utils.hideLoading()
      utils.showtoast('网络加载失败，刷新重试')
      this.setData({
        isSendBtn: true
      })
    })
  },
  cancel() {//弹窗取消
    this.closeModal('Model');
  },
  confirm() {
    if (this.data.isSendBtn) {
      this.setData({
        isSendBtn: false
      })
      //调用接口
      if (this.data.option.uuid !== '') {
        this.donateStep()
      } else {
        this.setData({
          isSendBtn: true
        })
        utils.showtoast('获取用户id失败')
      }
    }
  },
  gohone() {//去首页
    clearTimeout(timer);
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  checkMe() {
    var {
      option,
      getUserStep
    } = this.data
    if (option.uuid == (getUserStep && getUserStep.uuid)) {
      utils.showtoast('自己不能给自己赠送步数哦', null, 3000, true)
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }, 1500)
    }
  }

})